from pylons import config
from sqlalchemy import Column, MetaData, Table, types, ForeignKey
from sqlalchemy.orm import mapper, relation, backref
from sqlalchemy.orm import scoped_session, sessionmaker
import datetime
from xrisks.model.objects import *

# Global session manager.  Session() returns the session object appropriate for the current web request.
Session = scoped_session(sessionmaker(autoflush=True, transactional=True, bind=config['pylons.g'].sa_engine))

#print Session

# Global metadata. If you have multiple databases with overlapping table names, you'll need a metadata for each
# database.
metadata = MetaData()

# Define a table.
tableprefix=config.get('table.prefix')

risks_table = Table(tableprefix+"risks", metadata,
    Column("id", types.Integer, primary_key=True, autoincrement=True),
    Column("name", types.String, nullable=False),
    Column("description", types.TEXT),
    Column("probability", types.Float, default=0),
    Column("impact", types.Float, default=0),
    Column("created", types.DateTime, default=datetime.datetime.now()),
    Column("updated", types.DateTime, onupdate=datetime.datetime.now())
    )

impacts_table = Table(tableprefix+"impacts", metadata,
    Column("id", types.Integer, primary_key=True, autoincrement=True),
    Column("risk_id", types.Integer, ForeignKey(tableprefix + 'risks.id')),
    Column("name", types.String, nullable=False),
    Column("description", types.TEXT),
    Column("created", types.DateTime, default=datetime.datetime.now()),
    Column("updated", types.DateTime, onupdate=datetime.datetime.now())
    )

indicators_table = Table(tableprefix+"indicators", metadata,
    Column("id", types.Integer, primary_key=True, autoincrement=True),
    Column("risk_id", types.Integer, ForeignKey(tableprefix + 'risks.id')),
    Column("name", types.String, nullable=False),
    Column("description", types.TEXT),
    Column("created", types.DateTime, default=datetime.datetime.now()),
    Column("updated", types.DateTime, onupdate=datetime.datetime.now())
    )

strategies_table = Table(tableprefix+"strategies", metadata,
    Column("id", types.Integer, primary_key=True, autoincrement=True),
    Column("risk_id", types.Integer, ForeignKey(tableprefix + 'risks.id')),
    Column("name", types.String, nullable=False),
    Column("description", types.TEXT),
    Column("created", types.DateTime, default=datetime.datetime.now()),
    Column("updated", types.DateTime, onupdate=datetime.datetime.now())
    )

actions_table = Table(tableprefix+"actions", metadata,
    Column("id", types.Integer, primary_key=True, autoincrement=True),
    Column("risk_id", types.Integer, ForeignKey(tableprefix + 'risks.id')),
    Column("name", types.String, nullable=False),
    Column("description", types.TEXT),
    Column("created", types.DateTime, default=datetime.datetime.now()),
    Column("updated", types.DateTime, onupdate=datetime.datetime.now())
    )

mapper(Risk, risks_table,
       properties={
                   'impacts':relation(Impact, backref=backref('risk', uselist=False), cascade="all, delete, delete-orphan"),
                   'indicators':relation(Indicator, backref=backref('risk', uselist=False), cascade="all, delete, delete-orphan"),
                   'strategies':relation(Strategy, backref=backref('risk', uselist=False), cascade="all, delete, delete-orphan"),
                   'actions':relation(Action, backref=backref('risk', uselist=False), cascade="all, delete, delete-orphan")
                   }
       )
mapper(Impact, impacts_table)
mapper(Indicator, indicators_table)
mapper(Strategy, strategies_table)
mapper(Action, actions_table)